/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName LogoutStageWebView.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.socialize.facebook
{
	
	import com.gesturesoft.imultimax.socialize.org.osflash.signals.Signal;
	
	import flash.display.Stage;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.LocationChangeEvent;
	import flash.events.StageOrientationEvent;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	
	import mx.core.UIComponent;
	
	public class LogoutStageWebView extends UIComponent
	{
		private var stageWebView:StageWebView;
		private var myStage:Stage;
		private var __url:String;
		private var __text:String;
		
		public var appUrl:String;
		
		public var completeSignal:Signal = new Signal();
		
		public function set url(url:String):void
		{
			__url = url;
			
			if(stageWebView){
				stageWebView.loadURL(url);
			}
		}
		
		public function set text(text:String):void
		{
			__text = text;
			
			if(stageWebView){
				stageWebView.loadString(text);
			}
		}
		
		public function LogoutStageWebView()
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		protected function addedToStageHandler(event:Event):void
		{
			myStage = stage;
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			measureBox();
			hide();
		}
		
		protected function measureBox():void
		{
			stageWebView = new StageWebView();
			stageWebView.stage = myStage;
			stageWebView.viewPort = new Rectangle(0, 0, 20, 20);
			stageWebView.addEventListener(ErrorEvent.ERROR, errorHandler);
			stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,handleLocationChange);
			
			if (__url) {
				stageWebView.loadURL(__url);
			} else if (__text) {
				stageWebView.loadString(__text);
			}
			
		}
		
		protected function handleLocationChange(event:Event):void
		{
			trace("Enter Location Change Logout");
			if (stageWebView && event.target.location==appUrl)
			{
				stageWebView.removeEventListener(Event.LOCATION_CHANGE,handleLocationChange);
				completeSignal.dispatch(event.clone());
			}
			
			
		}
		
		public function hide():void{
			stageWebView.stage = null;
		}
		
		public function show():void{
			stageWebView.stage = myStage;
		}
		
		public function dispose():void{
			hide();
			stageWebView.dispose();
		}
		
		protected function errorHandler(event:Event):void
		{
			dispatchEvent(event.clone());
		}
	}
}